package com.hccl.util;

import com.hccl.exception.updateFileOperateException;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
/**
 * 文件常用操作类
 */
public class FileOperations {
    private final static Logger LOG = LoggerFactory.getLogger(FileOperations.class);
    /**
     *
     * @param path 文件或文件夹的路径
     *             1. 文件或文件夹路径以bak结尾则说明是回退文件，执行回退操作
     *             2. 文件或文件夹路径不以bak结尾，则说明是备份文件，执行备份操作
     */
    public static void reNameFile(String path) throws updateFileOperateException {

        try {
            if (path.endsWith("bak")) {
                if (path.endsWith(".bak")) {
                    String path1 = path.substring(0, path.length() - 4);
                    cpFile(path, path1);
                } else {
                    String path2 = path.substring(0, path.length() - 3);
                    cpFile(path, path2);
                }
            } else {
                if (Files.isDirectory(Paths.get(path))) {
                    String path3 = path.substring(0, path.length() - 1);
                    cpFile(path, path3 + "bak");
                } else {
                    cpFile(path, path + ".bak");
                }
            }
        }catch (Exception e){
            throw new updateFileOperateException("更新失败！原因：备份/恢复文件失败。");
        }
    }
    /**
     * 重命名文件或文件夹
     * @param src 源文件或文件夹
     * @param des 目的文件或目的文件夹
     */
    public static void cpFile(String src,String des)  throws updateFileOperateException{

        try {
            if (!Files.isDirectory(Paths.get(src))) {
                Files.copy(Paths.get(src), Paths.get(des), StandardCopyOption.REPLACE_EXISTING);
            } else {
                if (!Files.exists(Paths.get(des))) {
                    Files.createDirectories(Paths.get(des));
                }
                File file = new File(src);
                File[] files = file.listFiles();
                if (file.exists()) {
                    for (File f : files) {
                        cpFile(f.getAbsolutePath(),des + "/" + f.getName());
//                        Files.copy(Paths.get(f.getAbsolutePath()), Paths.get(des + "/" + f.getName()), StandardCopyOption.REPLACE_EXISTING);
                    }
                }
            }
        }catch (Exception e){
            throw new updateFileOperateException("更新失败！原因：复制文件失败。");
        }
    }

    public static void delete(String path) throws updateFileOperateException {
        File file = new File(path);
        try{
            if (file.isDirectory()) {
                if (file.list().length == 0) {
                    file.delete();
                }else {
                    String files[] = file.list();
                    for (String temp : files) {
                        String subFile = path +"/" +temp;
                        delete(subFile);
                    }
                    if (file.list().length == 0) {
                        file.delete();
                    }
                }
            } else {
                file.delete();
            }
        }catch(Exception e){
            throw new updateFileOperateException("更新失败！原因：删除文件失败。");
        }
    }

    public static void deleteDirectory(String path)throws IOException{
        File file = new File(path);
        FileUtils.deleteDirectory(file);

    }
}
